﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint;
using Slam;
using Slam.Configuration;
using System.Data.Linq;
//using SlamCms.SharePoint.SmartFields;
using SlamCms.SharePoint.Tagging.Services;
using SlamCms.Common;
using SlamCms.SharePoint.FieldTypes;
using SlamCms.SharePoint.Core;
using Slam.FieldTypes;

namespace SlamCms.SharePoint.Tagging.FieldTypes
{
	public partial class TagFieldEditor : UserControl, IFieldEditor
	{	
		#region IFieldEditor Members

		public bool DisplayAsNewSection
		{
			get { return true; }
		}

		public void InitializeWithField(SPField field)
		{
			SlamField tagField = field as SlamField;

			if (!Page.IsPostBack)
			{
				var tagCategories = SlamCmsContext.Data.TagCategories;
				if (tagCategories.Count > 0)
				{
					ddlTagCategories.DataTextField = "Title";
					ddlTagCategories.DataValueField = "TagCategoryId";
					ddlTagCategories.DataSource = tagCategories;
					ddlTagCategories.DataBind();
				}

				ddlDisplayModes.DataSource = Enum.GetNames(typeof(TagFieldDisplayMode));
				ddlDisplayModes.DataBind();

				TagFieldDisplayMode displayMode = TagFieldDisplayMode.Modal;
				bool defaultRequired = false;
				string tagCategoryId = "";
				if (tagField != null)
				{
					
					if (tagField.GetCustomPropertyValue("DisplayMode") != null)
					{
						displayMode = (TagFieldDisplayMode)tagField.GetCustomPropertyValue("DisplayMode");
					}
					
					if (tagField.GetCustomPropertyValue("DefaultRequired") != null)
					{
						defaultRequired = (bool)tagField.GetCustomPropertyValue("DefaultRequired");
					}
					tagCategoryId = (string)tagField.GetCustomPropertyValue("TagCategoryId");

					txtDefaultValue.Text = tagField.DefaultValue;
				}

				chkDefaultRequired.Checked = defaultRequired;

				ListItem item = ddlTagCategories.Items.FindByValue(tagCategoryId);
				if (item != null)
				{
					item.Selected = true;
				}

				item = ddlDisplayModes.Items.FindByValue(displayMode.ToString());
				if (item != null)
				{
					item.Selected = true;
				}
				else
				{
					ddlDisplayModes.SelectedIndex = 0;
				}
			}
		}

		public void OnSaveChange(SPField field, bool isNewField)
		{
			string lookupList = "";
			SlamCmsContext.GlobalSettings(gs =>
			{
				SPList tags = gs.Lists.TryGetList(Constants.ListNames.Tags);
				if (tags != null)
				{
					lookupList = tags.ID.ToString();
				}
			});

			SlamField tagField = field as SlamField;
			tagField.SetCustomProperty("TagCategoryId", typeof(string), ddlTagCategories.SelectedItem.Value);
			tagField.SetCustomProperty("DefaultRequired", typeof(bool), chkDefaultRequired.Checked);
			tagField.SetCustomProperty("DisplayMode", typeof(TagFieldDisplayMode), (TagFieldDisplayMode)Enum.Parse(typeof(TagFieldDisplayMode), ddlDisplayModes.SelectedValue));
			tagField.DefaultValue = txtDefaultValue.Text;
			tagField.SetCustomProperty("LookupList", typeof(string), lookupList);
		}

		#endregion
	}
}